<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/1/26
 * Time: 16:23
 */

namespace app\common\service;


use app\common\enum\ConfigEnum;
use app\common\helper\CommonHelper;
use yii\db\BaseActiveRecord;
use yii\helpers\ArrayHelper;
use yii\db\ActiveRecord;
use yii\helpers\Json;

class OptionMap
{
    public $map;
    /**
     * @var ActiveRecord|null $model
     */
    public $model;//单项记录，存在于更新记录

    /**
     * @param ActiveRecord|null $model
     */
    public function setModel($model)
    {
        $this->model = $model;
    }

    public function getModel(){
        return $this->model;
    }
    /**
     * @return mixed
     */
    public function getMap()
    {
        return $this->map;
    }

    public function setSearchConfigOptions(&$config){
        if(empty($config)){
            return $config;
        }

        $map = $this->getMap();

        foreach ($config as  &$item){
            if(isset($item[ConfigEnum::FORCE_OPTIONS])){
                $item['options'] = $item[ConfigEnum::FORCE_OPTIONS];
                continue;
            }
            $field = $item['field'];
            if(isset($map[$field])){
                $map_item = $map[$field];
                if(($map_item instanceof OptionMapItem) && is_callable($map_item->getWhere()) && $this->getModel()){
                    $wherecall = $map_item->getWhere();
                    $where = $wherecall($this->getModel());
                    $map_item->setWhere($where);
                }
                $item['options'] = OptionsHandel::createInstance($map_item)->getOptions();
            }
        }
        return $config;
    }

    public function replaceOptions($data){
        if(empty($data)){
            return $data;
        }
        if($data instanceof ActiveRecord){
            $data = $data->toArray();
        }

        $map = $this->getMap();

        foreach ($data as  $field => &$item){
            if(isset($map[$field])){
                $map_item = $map[$field];
                if($map_item instanceof OptionMapItem){
                    $map_type = $map_item->getType();
                    $field_id = $map_item->getId();
                    $is_leaf = $map_item->isLeaf();
                }else{
                    $map_type = ArrayHelper::getValue($map_item,'type');
                    $field_id = ArrayHelper::getValue($map_item,'id','id');
                    $is_leaf = ArrayHelper::getValue($map_item,'is_leaf');
                }
                if($map_type == 'tree'){
                    //树形结构
                    $options = OptionsHandel::createInstance($map_item)->getOptions();
                    $item = CommonHelper::treeFindName($options,$item,$field_id,true,$is_leaf);
                }else{
                    $options = OptionsHandel::createInstance($map_item)->getOptions();
                    try{
                        $arr_json = Json::decode($item);
                        if(JSON_ERROR_NONE === json_last_error() && is_array($arr_json)){
                            //表示是json结构,理论上是一维数组
                            foreach ($arr_json as $k => $value){
                                $arr_json[$k] = ArrayHelper::getValue($options,$value);
                            }
                            $item = implode(",",$arr_json);
                        }else{
                            $item = ArrayHelper::getValue($options,$item);
                        }
                    }catch (\Throwable $e){
                        $item = ArrayHelper::getValue($options,$item);
                    }

                }
            }
        }
        return $data;
    }

    public static $obj;
    public static function getInstance(){
        return static::$obj instanceof OptionMap ? static::$obj : static::$obj = new static();
    }
}